const DB = require('../../utils/db');
module.exports = {
	list: async (pageNum, pageSize) => {
		let [allList] = await DB.query(`select * from t_album`);
		let arr = [(pageNum - 1) * pageSize, pageSize];

		let sql = `select a.*, b.nums, date_format(a.createTime,'%Y-%m-%d') as createTime ,date_format(a.updateTime,'%Y-%m-%d') as updateTime  
                   from t_album a left join(SELECT albumId,  count(1) nums FROM t_pic GROUP BY albumId) b on a.id=b.albumId limit ?,?`;
		let [list] = await DB.query(sql, arr);
		list.forEach((item) => (item.password = ''));
		return { data: list, total: allList.length };
	},
	listById: async (id) => {
		let sql = `select * from t_pic where albumId=?`;

		let [list] = await DB.query(sql, [id]);
		return list;
	},
	pwd: async (id, password) => {
		// 查看是否需要密码
		const [albumList] = await DB.query('select * from t_album where id = ?', [id]);
		if (albumList[0].isPwd == 1) {
			if (!password) throw new Error('需要密码');
			if (albumList[0].password != password) throw new Error('密码错误');
		}
		let sql = `select count(1) total from t_pic where albumId=?`;

		let [list] = await DB.query(sql, [id]);
		return list[0];
	}
};
